<?php
namespace app\controllers;


use app\common\corelib\LocalUpload;
use app\logic\SentenceLogic;
use yii;
//美句管理
class SentenceController extends BackendController
{
    //中文美句
    public function actionChiness()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $sentenceLogic = new SentenceLogic();
        $this->data['breadcrumbs'] = '中文打卡管理';
        return $this->render('index', [
            'sentence_type'=>0,
            'sentence' => $sentenceLogic->getSentence(0,$query, $page),
            'query' => $query
        ]);
    }
    //西语美句
    public function actionSpanish()
    {
        $query = Yii::$app->request->get('form', []);
        $page = Yii::$app->request->get('page', 1);
        $sentenceLogic = new SentenceLogic();
        $this->data['breadcrumbs'] = '西文打卡管理';
        return $this->render('index', [
            'sentence_type'=>1,
            'sentence' => $sentenceLogic->getSentence(1,$query, $page),
            'query' => $query
        ]);
    }
    //添加美句
    public function actionAdd()
    {
        $sentenceLogic = new SentenceLogic();
        if (!Yii::$app->request->post()) {
            $sentence_type = Yii::$app->request->get('sentence_type', 0);
            if ($sentence_type == 0)
                $this->data['breadcrumbs'] = ['中文打卡管理' => U('sentence/chiness'), ' 添加美句' => ''];
            elseif ($sentence_type == 1)
                $this->data['breadcrumbs'] = ['西文打卡管理' => U('sentence/spanish'), ' 添加美句' => ''];
            return $this->render('add', [
                'sentence_type' => $sentence_type,
                'theme' => '/assets/',
                'actionUrl' => U('sentence/add')
            ]);
        } else {
            $params = Yii::$app->request->post('form');
            $logicRes = $sentenceLogic->addSentence($params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success', '新增美句成功');
                if ($params['sentence_type'] == 0)
                    jump('sentence/chiness');
                elseif ($params['sentence_type'] == 1)
                    jump('sentence/spanish');
            } else {
                Yii::$app->getSession()->setFlash('error', '新增文章失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }
    //编辑美句
    public function actionEdit(){
        $sentenceLogic = new SentenceLogic();
        if(!Yii::$app->request->post()){
            $id = Yii::$app->request->get('sentence_id');
            $sentence = $sentenceLogic->getById($id);
            if($sentence['sentence_type']==0)
                $this->data['breadcrumbs'] = ['中文打卡管理' => U('sentence/chiness'), $sentence['sentence_name'] => '', '更新' => ''];
            elseif($sentence['sentence_type']==1)
                $this->data['breadcrumbs'] = ['西文打卡管理' => U('sentence/spanish'), $sentence['sentence_name'] => '', '更新' => ''];
            return $this->render('edit', [
                'sentence' => $sentence,
                'theme' => '/assets/',
                'actionUrl' => U('sentence/edit'),
            ]);
        }else{
            $params = Yii::$app->request->post('form');
            $id = Yii::$app->request->post('sentence_id');
            $sentence = $sentenceLogic->getById($id);
            $logicRes = $sentenceLogic->editById($id, $params);
            if ($logicRes) {
                Yii::$app->getSession()->setFlash('success','更新美句成功');
                if($sentence['sentence_type']==0)
                    jump('sentence/chiness');
                elseif($sentence['sentence_type']==1)
                    jump('sentence/spanish');
            } else {
                Yii::$app->getSession()->setFlash('error','更新美句失败');
                $this->goBack(Yii::$app->request->referrer);
            }
        }
    }
    //删除美句
    public function actionDelete(){
        $id = Yii::$app->request->get('sentence_id');
        $sentenceLogic = new SentenceLogic();
        $sentence = $sentenceLogic->getById($id);
        $logicRes = $sentenceLogic->delete($id);
        if ($logicRes) {
            Yii::$app->getSession()->setFlash('success','删除成功');
        } else {
            Yii::$app->getSession()->setFlash('error','删除失败');
        }
        if($sentence['sentence_type']==0)
            return $this->redirect(U('sentence/chiness'));
        elseif($sentence['sentence_type']==1)
            return $this->redirect(U('sentence/spanish'));
    }

}